package com.usian.controller;

import com.usian.pojo.Etc;
import com.usian.pojo.User;
import com.usian.service.EtcService;
import com.usian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@RequestMapping("/etc")
@Controller
public class EtcController {

    @Autowired
    private EtcService etcService;

    @Autowired
    private UserService userService;

    @RequestMapping("/UserNameTo")
    public String UserNameTo(String userName,HttpServletRequest request,Model model){
        User user = userService.findByUserName(userName);
        if (user.getRoleId()==1){//管理员
            return "redirect:/etc/findList";
        }else {
            Etc etc = new Etc();
            etc.setUserName(userName);
            return this.findAll(etc,request,model);
        }
    }

    @RequestMapping("/findList")
    public String findList(Etc etc, Model model){//管理人员的查询 -- 没有增删改
        //因为 管理人员只能查询 并且还有 carId 和 etcId 的模糊查询，所以需要进行带条件
        List<Etc> etcList = etcService.findAllBy1(etc);
        model.addAttribute("etcList",etcList);
        model.addAttribute("etc",etc);
        return "index1";
    }

    @RequestMapping("/findAll")
    public String findAll(Etc etc,HttpServletRequest request, Model model){//普通人员的查询 -- 有增删改
        model.addAttribute("etc",etc);
        model.addAttribute("userName",request.getAttribute("userName"));
        List<Etc> etcList = etcService.findAll(etc);
        model.addAttribute("etcList",etcList);
        return "index";
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model,String userName){
        model.addAttribute("userName",userName);
        return "add";
    }

    @RequestMapping("add")
    public String  add(RedirectAttributes redirectAttributes,Etc etc) throws IOException {
        etcService.add(etc);
        redirectAttributes.addAttribute("userName", etc.getUserName());
        return "redirect:/etc/findAll";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Model model,Integer id){
        Etc etc = etcService.findById(id);
        model.addAttribute("etc",etc);
        return "update";
    }

    @RequestMapping("update")
    public String  update(RedirectAttributes redirectAttributes, Etc etc) throws IOException {
        etcService.update(etc);
        redirectAttributes.addAttribute("userName", etc.getUserName());
        return "redirect:/etc/findAll";
    }

    @RequestMapping("deleteById")
    public String  deleteById(RedirectAttributes redirectAttributes,Integer id) throws IOException, ServletException {
        Etc etc = etcService.findById(id);
        etcService.delete(id);
        redirectAttributes.addAttribute("userName", etc.getUserName());
        return "redirect:/etc/findAll";
    }

}
